<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar></custom-nav-bar>
		<van-tabs v-model:active="active" color="#ff4d4f" background="#F8F8FF" title-active-color="#ff4d4f"
			title-inactive-color="#999" @change="onChange" offset-top="18px" sticky>
			<van-tab title="关注"></van-tab>
			<van-tab title="推荐">
				<view class="banner">
					<swiper indicator-dots="true" indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff"
						autoplay="true" circular="true">
						<swiper-item v-for="item in 4">
							<image :src="getImage('/banner/banner1.jpg')" mode="aspectFill"></image>
						</swiper-item>
					</swiper> 
				</view>

				<view class="item">
					<view class="box" v-for="(hot, index) in hotList" :key="index" @click="goPost(hot.dishId)">
						<view class="item-content">
							<image :src="getImage2(hot.imageUrl)" mode="aspectFill" class="thumbnail"></image>
							<view class="count-info">
								<view class="counts">
									<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
									<text>{{ hot.views }}</text>
								</view>
								<view class="counts">
									<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
									<text>{{ hot.likes }}</text>
								</view>
								<view class="counts">
									<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
									<text>{{ hot.favorites }}</text>
								</view>
							</view>
							<view class="item-info">
								<view class="title">{{ hot.name }}</view>
								<view class="user">
									<uni-icons type="person" color="#888" size="18"></uni-icons>
									<text>{{ hot.username }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</van-tab>
			<van-tab title="热门">
				<lazy-component>
					<van-back-top right="10vw" bottom="10vh" offset=800 />
					<view class="item">
						<view class="box" v-for="(hot, index) in hotList" :key="index" @click="goPost(hot.dishId)">
							<view class="item-content">
								<image :src="getImage2(hot.imageUrl)" mode="aspectFill" class="thumbnail"></image>
								<view class="count-info">
									<view class="counts">
										<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
										<text>{{ hot.views }}</text>
									</view>
									<view class="counts">
										<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
										<text>{{ hot.likes }}</text>
									</view>
									<view class="counts">
										<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
										<text>{{ hot.favorites }}</text>
									</view>
								</view>
								<view class="item-info">
									<view class="title">{{ hot.name }}</view>
									<view class="user">
										<uni-icons type="person" color="#888" size="18"></uni-icons>
										<text>{{ hot.username }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</lazy-component>
			</van-tab>
			<van-tab title="分类"></van-tab>
		</van-tabs>
	</view>

</template>

<script setup>
	import {
		ref,
		createApp,
		onMounted
	} from 'vue';
	import {
		Tab,
		Tabs,
		Lazyload,
		Button,
		Loading,
		PullRefresh,
		BackTop
	} from 'vant';
	import { appUrl } from '../../main';


	let active = ref(1);
	const getImage = (path) => {
		return "https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/image" + path
	}
	const getImage2 = (path) => {
		return "https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/train/data/" + path
	}
	let hotList = ref([]);

	const onChange = (index) => {
		if (index == 0) {
			console.log('当前激活的标签页索引:', index);
		} else if (index == 1) {
			getHotList();
		} else if (index == 2) {
			getHotList();
		} else {
			console.log('当前激活的标签页索引:', index);
		}
	}

	const getHotList = async () => {
		try {
			uni.request({
				url: appUrl+'/dish/Hotdish',
				data: {
					page: 1,
					limit: 10
				},
				method: "POST"
			}).then((res) => {
				if (res.data.status == 200) {
					hotList = res.data.data.dishes
				} else {
					console.log('服务异常');
				}
			})
		} catch (error) {
			console.error('请求失败:', error);
		}
	}

	const items = [{
			image: '/select/chuancai.jpeg',
			title: '距离赛博2077真的还需要50多年吗?',
			subtitle: '张三',
			likes: 555,
			favorites: 487,
			views: 436,
			url: '/pages/detail/detail?id=1'
		},
		{
			image: '/select/chuancai.jpeg',
			title: '距离赛博2077真的还需要50多年吗?',
			subtitle: '李四',
			likes: 555,
			favorites: 487,
			views: 436,
			url: '/pages/detail/detail?id=1'
		}
		// ...其他项目
	];
	
	const goPost = (id) => {
		uni.setStorageSync('dishId', id)
		uni.navigateTo({
			url:"/pages/PostView/PostView"
		})
	}
	
	onMounted(() => {
		getHotList();
	})
</script>

<style lang="scss" scoped>
	
	.banner {
		width: 750rpx;
		padding: 30rpx 0;

		swiper {
			width: 750rpx;
			height: 340rpx;

			&-item {
				width: 100%;
				height: 100%;
				padding: 0 30rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}

	::v-deep .van-tab__text {
		font-size: 15px !important;
	}
	
	::v-deep .van-back-top {
	  background-color: #ff4d4f;
	}

	.item {
		margin-top: 30rpx;
		padding: 0 30rpx;
		display: grid;
		grid-template-columns: 1fr 1fr;
		column-gap: 20rpx;
		row-gap: 20rpx;

		.box {
			height: 480rpx;
			border-radius: 12rpx;
			overflow: hidden;
			position: relative;
			background-color: #FFFFFF;

			.count-info {
				position: absolute;
				bottom: 155rpx;
				left: 0;
				display: flex;
				align-items: center;
				padding: 10rpx;
				background-color: rgba(0, 0, 0, 0);
				color: white;
				font-size: 13px;
				gap: 18rpx;

				.counts {
					display: flex;
					gap: 4rpx;
					align-items: center;
				}
			}

			.thumbnail {
				width: 100%;
				height: 320rpx;
			}

			.item-info {
				margin-left: 10rpx;

				.title {
					font-size: 16px;
					color: #333;
					height: 100rpx;
				}

				.user {
					display: flex;
					font-size: 14px;
					color: #999;
					gap: 10rpx;
					align-items: center;
				}
			}
		}
	}
</style>